package parking;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * This class holds a logger which enables to write in a log file
 * @author Sophie and Ronnie
 * @date 9 November 2013
 */
public class LogManager {

	/**
	 * Custom class created to create the logs.
	 * By default Java Logger put the date and all the methods invoked in a log.
	 * Too bulky, had to create a custom one.
	 * @author Sophie and Ronnie
	 * @date 12 November 2013
	 */
	public class MyCustomFormatter extends Formatter {   
	    public MyCustomFormatter() { 
	    	super(); 
	    }

	    @Override 
	    public String format(final LogRecord record) {
	        return record.getMessage() + "\n";
	    } 
	}
	
	private Logger logger;						//object to log messages for the application
	private FileHandler fileTxt;				//responsible for writing in a specified file 	
	private MyCustomFormatter customFormatter;	//custom formatter created to remove the date
	
	/**
	 * Constructor for the LogManager class
	 * Is responsible for creating the file too
	 * @param nomFichier : name of the file
	 * @param myLevel : level of severity of messages
	 */
	public LogManager(String nomFichier, Level myLevel) {
		this.logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
		this.logger.setLevel(myLevel);
		
		try {
			this.fileTxt = new FileHandler(nomFichier);
			
			// create txt Formatter
		    //formatterTxt = new SimpleFormatter();
			customFormatter = new MyCustomFormatter();
		    fileTxt.setFormatter(customFormatter);
		    logger.addHandler(fileTxt);
		    
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @return the logger
	 */
	public Logger getlogger() {
		return this.logger;
	}

}
